﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;


namespace Net.Class
{
    class NetConfig
    {
        public static void addItem()  //测试用
        {
            string cfgfile = AppDomain.CurrentDomain.BaseDirectory + "\\Appconfig";
            if (!System.IO.File.Exists(cfgfile))
                System.IO.File.Create(cfgfile).Close();
            string connStringName = "abc";
            Configuration config = ConfigurationManager.OpenExeConfiguration(cfgfile);
            ConnectionStringSettings setting = config.ConnectionStrings.ConnectionStrings[connStringName];
            if (setting == null)
            {
                setting = new ConnectionStringSettings();
                setting.Name = connStringName;
                setting.ConnectionString = "12313132";
                config.ConnectionStrings.ConnectionStrings.Add(setting);
                config.Save();
            }  

        }

        /**/
        /// <summary>
        /// 保存appSettings中某key的value值
        /// </summary>
        /// <param name="strKey">key's name</param>
        /// <param name="newValue">value</param>
        public static void AppSettingsSave(string strKey, string newValue)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //if (AppSettingsKeyExists(strKey, config))
            //{
            //    config.AppSettings.Settings[strKey].Value = newValue;
            //    config.Save(ConfigurationSaveMode.Modified);
            //    ConfigurationManager.RefreshSection("appSettings");
            //}
            //else
            //{
                //ConnectionStringSettings setting = new ConnectionStringSettings();
                //setting.Name = strKey;
                //setting.ConnectionString = newValue;
                //config.ConnectionStrings.ConnectionStrings.Add(setting);
                //config.Save();
                ConfigSectionData data = new ConfigSectionData();
                data.userName = strKey;
                data.password = newValue;
                //config.Sections.Add("add", data);
                String groupName = "users";
                ConfigurationSectionGroup group1 = config.SectionGroups[groupName];
                if (group1 == null)
                    config.SectionGroups.Add(groupName, new ConfigurationSectionGroup());
                group1 = config.SectionGroups[groupName];
                //ConfigSectionData add = config.SectionGroups[groupName].Sections["user"] as ConfigSectionData;
                //if (add == null)
                //    config.SectionGroups[groupName].Sections.Add("user", data);
                //else
                //{
                group1.Sections.Remove("user");
                group1.Sections.Add("user", data);
                //}
               // config.SectionGroups["group1"].Sections.Add("add", data);
                config.Save(ConfigurationSaveMode.Minimal);

            //}
        }
        /**/
        /// <summary>
        /// 判断appSettings中是否有此项
        /// </summary>
        private static bool AppSettingsKeyExists(string strKey, Configuration config)
        {
            foreach (string str in config.AppSettings.Settings.AllKeys)
            {
                if (str == strKey)
                {
                    return true;
                }
            }
            return false;
        }

        /**/
        /// <summary>
        /// 获取AppSettings配置节中的Key值
        /// </summary>
        /// <param name="keyName">Key's name</param>
        /// <returns>Key's value</returns>
        public static string GetAppSettingsKeyValue(string keyName)
        {
            return ConfigurationManager.AppSettings.Get(keyName);
        }

        /**/
        /// <summary>
        /// 获取ConnectionStrings配置节中的值
        /// </summary>
        /// <returns></returns>
        public static string GetConnectionStringsElementValue()
        {
            ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];
            return settings.ConnectionString;
        }

        /**/
        /// <summary>
        /// 保存节点中ConnectionStrings的子节点配置项的值
        /// </summary>
        /// <param name="elementValue"></param>
        public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("connectionStrings");
        }

        public static void removeUser(string userName)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            String groupName = "users";
            config.SectionGroups[groupName].Sections.Remove(userName);

        }

        public static String getAllUsers()
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            String groupName = "users";
            ConfigurationSectionGroup users = config.SectionGroups[groupName];
            ConfigurationSectionCollection sections = users.Sections;
            System.Collections.Specialized.NameObjectCollectionBase.KeysCollection keys = sections.Keys;
            keys.ToString();
            return keys.ToString();
               
        }

        public static string[] getUser()
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            String groupName = "users";
            ConfigurationSectionGroup group1 = config.SectionGroups[groupName];
            if (group1 == null)
                config.SectionGroups.Add(groupName, new ConfigurationSectionGroup());
            ConfigSectionData user = config.SectionGroups[groupName].Sections["user"] as ConfigSectionData;
            if (user != null)
            {
                string[] userInfo = new string[2];
                userInfo[0] = user.userName;
                userInfo[1] = user.password;
                return userInfo;
            }
            else
            {
                return null;
            }
        }


    }
}
